package com.openlogic.flow.service.impl;

import java.util.List;

import org.dromara.warm.flow.core.utils.StringUtils;
import org.dromara.warm.flow.orm.entity.FlowDefinition;
import org.dromara.warm.flow.orm.entity.FlowInstance;
import org.dromara.warm.flow.orm.entity.FlowUser;
import org.dromara.warm.flow.orm.mapper.FlowUserMapper;
import org.noear.solon.annotation.Component;

import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.solon.service.impl.ServiceImpl;
import com.openlogic.common.core.web.domain.entity.SysUser;
import com.openlogic.flow.domain.query.FlowTaskQuery;
import com.openlogic.flow.domain.vo.FlowTaskVo;
import com.openlogic.flow.service.IFlowCopyService;

/**
 * @ClassName: FlowCopyServcieImpl
 * @Description:  流程抄送接口服务实现
 * @author weiyongpeng
 * @date 2025年2月22日 上午9:21:52
 */
@Component
public class FlowCopyServcieImpl  extends ServiceImpl<FlowUserMapper, FlowUser> implements IFlowCopyService {

	@Override
	public List<FlowTaskVo> copyPage(FlowTaskQuery flowTaskQuery) {
		QueryWrapper wrapper = QueryWrapper.create()
				.select("c.nick_name AS approver,\r\n"
				+ "            b.flow_status,\r\n"
				+ "            b.business_id,\r\n"
				+ "            a.create_time,\r\n"
				+ "            b.node_name,\r\n"
				+ "            b.id ,\r\n"
				+ "            d.flow_name")
		.from(FlowUser.class).as("a")
		.leftJoin(FlowInstance.class).as("b").on("a.associated = b.id")
		.leftJoin(SysUser.class).as("c").on("b.create_by = c.user_id")
		.leftJoin(FlowDefinition.class).as("d").on("b.definition_id=d.id")
		.where("a.type = 4");
		
		if (flowTaskQuery.getFlowName() != null && StringUtils.isNotEmpty(flowTaskQuery.getFlowName())) {
			wrapper.like("c.nick_name",flowTaskQuery.getFlowName());
		}
		if (flowTaskQuery.getNodeName() != null && StringUtils.isNotEmpty(flowTaskQuery.getNodeName())) {
			wrapper.like("b.node_name",flowTaskQuery.getNodeName());
		}
		if (flowTaskQuery.getNodeType() != null) {
			wrapper.like("b.node_type",flowTaskQuery.getNodeType());
		}
		wrapper.orderBy(FlowUser::getCreateTime).desc();
		List<FlowTaskVo> list = listAs(wrapper, FlowTaskVo.class);
		return list;
	}

}
